/*
 *
 gcc glsphere.c -L. -lGLESv2 -lmingw32 -lSDL2_test -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_image && a 
 gcc glsphere.c -lSDL2_test -lSDL2_ttf -lm -lSDL2 && ./a.out
 gcc glsphere.c -llibGLESv2 -lSDL2_test -lm -lmingw32 -lSDL2main -lSDL2   && a
 adb push 2.c /sdcard/ &&
 Copyright (r) 1997-2014 Sam Lantinga <slouken@libsdl.org>

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely.
 */
#include "gles2base.h"
#include "SDL2/SDL.h"

void Draw ( UserData*userData)
{
	Update(userData,.01);
	// Set the viewport
	ctx.glViewport ( 0, 0, state->window_w, state->window_h);

	// Clear the color buffer
	ctx.glClear ( GL_COLOR_BUFFER_BIT );


	ctx.glCullFace ( GL_BACK );
	ctx.glEnable ( GL_CULL_FACE );

	// Use the program object
	ctx.glUseProgram ( userData->programObject );

	// Load the vertex position
	ctx.glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
			GL_FALSE, 0, userData->vertices );
	// Load the normal
	ctx.glVertexAttribPointer ( userData->normalLoc, 3, GL_FLOAT,
			GL_FALSE, 0, userData->normals );

	ctx.glEnableVertexAttribArray ( userData->positionLoc );
	ctx.glEnableVertexAttribArray ( userData->normalLoc );

	ctx.glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );

	// Bind the texture
	ctx.glActiveTexture ( GL_TEXTURE0 );
	ctx.glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId );
	/*ctx.glBindTexture ( GL_TEXTURE_2D, userData->textureId );*/

	// Set the sampler texture unit to 0
	ctx.glUniform1i ( userData->samplerLoc, 0 );

	ctx.glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices );
	/*ctx.glDrawElements ( GL_LINE_LOOP, userData->numIndices, GL_UNSIGNED_INT, userData->indices );*/

	/*eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );*/
}

int Init ( UserData*userData)
{
	GLbyte vShaderStr[] =  
		"attribute vec4 a_position;   \n"
		"attribute vec3 a_normal;     \n"
		"uniform mat4 u_mvpMatrix;    \n"
		"varying vec3 v_normal;       \n"
		"void main()                  \n"
		"{                            \n"
		"   gl_Position = u_mvpMatrix * a_position;  \n"
		"   v_normal = a_normal;      \n"
		"}                            \n";

	GLbyte fShaderStr[] =  
		"precision mediump float;                            \n"
		"varying vec3 v_normal;                              \n"
		"uniform samplerCube s_texture;                      \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  gl_FragColor = textureCube( s_texture, v_normal );\n"
		"}                                                   \n";

	// Load the shaders and get a linked program object
	userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );

	// Get the attribute locations
	userData->positionLoc = ctx.glGetAttribLocation ( userData->programObject, "a_position" );
	userData->normalLoc = ctx.glGetAttribLocation ( userData->programObject, "a_normal" );
	userData->mvpLoc = ctx.glGetUniformLocation( userData->programObject, "u_mvpMatrix" );

	// Get the sampler locations
	userData->samplerLoc = ctx.glGetUniformLocation ( userData->programObject, "s_texture" );

	SDL_Surface *surface = NULL;//IMG_Load("1.bmp");
	//SDL_SaveBMP(surface,"1.bmp");
	/*surface = NULL;*/
	// Load the texture
	//surface = SDL_LoadBMP("icon.bmp");
	/*userData->textureId = SDL_GL_LoadTexture(surface,NULL);*/
	userData->textureId = CreateSimpleTextureCubemap (NULL);
	if(surface)SDL_FreeSurface(surface);
	// Generate the vertex data
	userData->numIndices = esGenSphere ( 20, 0.75f, &userData->vertices, 
			&userData->normals, 
			&userData->texCoords, 
			&userData->indices );


	ctx.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	return 1;
}


